package com.api.gbhg.liuwenbo.api;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.liuwenbo.dao.DataBaseUtil;
import com.api.gbhg.liuwenbo.dao.NonCentralTreeDBUtil;
import com.api.gbhg.liuwenbo.dao.TreeStuctureDBUtil;
import com.api.gbhg.liuwenbo.pojo.NonCentralLog;
import com.api.gbhg.liuwenbo.pojo.NonCentralTree;
import com.api.gbhg.liuwenbo.pojo.RealIncome;
import com.api.gbhg.liuwenbo.utils.Utils;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.file.Prop;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.*;

/**
 * @Description 非中央组织架构
 * @Author lwb
 */

@Path("gb/NoCentralTree")
public class NonCentralTreeApi {

    private PropertyManagementCostDao rmcd=new PropertyManagementCostDao();
    private static String uf_fzysr;
    private static String uf_fzysrxm;
    private static String uf_fzysjsr;
    private static String dfbkjbzc;
    private static String dfbkxmzc;
    private int yjKey =0;
    private int ejKey =0;
    private int xmKey =0;
    private static List<String> sysadmin ;
    private static List<String> status1 ;
//    private static List<String> status2 ;
    private static List<String> status3 ;
    private static List<String> DepartmentStatus ;
    private static String leaderStr ;

    /**
     * 获取权限配置文件值
     */
    public void getJurisdiction(){
        uf_fzysr = Prop.getPropValue("NonCentralTree","uf_fzysr");
        uf_fzysrxm = Prop.getPropValue("NonCentralTree","uf_fzysrxm");
        uf_fzysjsr = Prop.getPropValue("NonCentralTree","uf_fzysjsr");
        dfbkjbzc = Prop.getPropValue("NonCentralTree","dfbkjbzc");
        dfbkxmzc = Prop.getPropValue("NonCentralTree","dfbkxmzc");
        sysadmin = Arrays.asList(Prop.getPropValue("NonCentralTree","sysadmin"));
        DepartmentStatus = Arrays.asList(Prop.getPropValue("NonCentralTree","DepartmentStatus").split(","));
        status1 = Arrays.asList(Prop.getPropValue("NonCentralTree","status1").split(","));
//        status2 = Arrays.asList(Prop.getPropValue("NonCentralTree","status2").split(","));
        status3 = Arrays.asList(Prop.getPropValue("NonCentralTree","status3").split(","));
        leaderStr = Prop.getPropValue("NonCentralTree","leaderStr");
    }

    //--------------------------------------------------------------------------------------------------------------------------
    /**
     * 展示非中央组织结构
     * @return
     */
    @POST
    @Path("/DisplayCtTree")
    @Produces(MediaType.APPLICATION_JSON)
    public String DisplayCtTree(Map<String,Object> param){
        this.getJurisdiction();
        Map<String,Map<String,List<NonCentralTree>>> map = queryCtTree(param);

        List<NonCentralTree> list = new ArrayList<>();
        int i = 0;
        int j = 0;

        for (Map.Entry<String,Map<String,List<NonCentralTree>>> entry: map.entrySet()) {
            NonCentralTree tree = new NonCentralTree();
            tree.setTitle(entry.getKey());
            tree.setKey(String.valueOf(i));
            List<NonCentralTree> listdw = new ArrayList<>();
            for (Map.Entry<String,List<NonCentralTree>> entrydw: entry.getValue().entrySet()) {
                NonCentralTree treedw = new NonCentralTree();
                String[] s = entrydw.getKey().split("_");
                treedw.setTitle(s[0]);
                treedw.setKey("0-"+j);
//                List<NonCentralTree> fill = fill(entrydw.getValue());
//                List<NonCentralTree> sortFill = sortFl(fill);
//                treedw.setChildren(sortFill);
                List<NonCentralTree> newlist = upFramework(entrydw.getValue());
                List<NonCentralTree> sortlist = sortFl(newlist);
                treedw.setChildren(sortlist);
                treedw.setDwId(s[1]);
                treedw.setNf(s[2].split("@")[0]);
                listdw.add(treedw);
                j++;
            }
            tree.setChildren(listdw);
            list.add(tree);
            i++;
        }


        return JSON.toJSONString(list);
    }


    /**
     * 重组架构(针对地方财政)
     * @param list
     * @return
     */
    public List<NonCentralTree> upFramework(List<NonCentralTree> list){
        int index =0;
        boolean count = true;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getSzfl().equals(dfbkjbzc) || list.get(i).getSzfl().equals(dfbkxmzc)){
                if (count){
                    List<NonCentralTree> newList = new ArrayList<>();
                    NonCentralTree newtree = new NonCentralTree();
                    newtree.setKey("0-0-0-"+ejKey);
                    newtree.setTitle(list.get(i).getTitle());
                    newtree.setId(list.get(i).getId());
                    newtree.setSzfl(list.get(i).getSzfl());
                    newtree.setNf(list.get(i).getNf());
                    newtree.setDwId(list.get(i).getDwId());
                    newtree.setYjsr(list.get(i).getYjsr());
                    newtree.setYqndyjjzje(list.get(i).getYqndyjjzje());
                    newtree.setYqndsjjzje(list.get(i).getYqndsjjzje());
                    newtree.setSjsr(list.get(i).getSjsr());
                    newtree.setGnkm(list.get(i).getGnkm());
                    newtree.setDw(list.get(i).getDw());
                    newtree.setSfwdlhsdw(list.get(i).getSfwdlhsdw());
                    newtree.setChildren(list.get(i).getChildren());
                    newtree.setGnkmmc(list.get(i).getGnkmmc());
                    newtree.setYhpje(list.get(i).getYhpje());
                    newtree.setYjsrsy(list.get(i).getYjsrsy());
                    newtree.setYqndyjjzyyje(list.get(i).getYqndyjjzyyje());
                    newtree.setYqndyjjzsyje(list.get(i).getYqndyjjzsyje());
                    newtree.setYqndsjjzsyje(list.get(i).getYqndsjjzsyje());
                    newtree.setXmdm(list.get(i).getXmdm());
                    newtree.setYssrfl(list.get(i).getYssrfl());

                    if (list.get(i).getSzfl().equals(dfbkjbzc)){
                        newtree.setDfzcbkjbzc(list.get(i).getYjsr());
                    }else{
                        newtree.setDfzcbkxmzc(list.get(i).getYjsr());
                    }

                    ejKey++;
                    index=i;

                    newList.add(newtree);
                    list.get(i).setKey("0-0-"+yjKey);
                    yjKey++;
                    list.get(i).setTitle("地方财政拨款");
                    list.get(i).setChildren(newList);

                    list.get(i).setSjsr("");
                    list.get(i).setYjsr("");
                    list.get(i).setYqndsjjzje("");
                    list.get(i).setYqndyjjzje("");
                    count = false;
                }else{
                    List<NonCentralTree> newList = new ArrayList<>();
                    NonCentralTree newtree = new NonCentralTree();
                    newtree.setKey("0-0-0-"+ejKey);
                    ejKey++;
                    newtree.setTitle(list.get(i).getTitle());
                    newtree.setId(list.get(i).getId());
                    newtree.setSzfl(list.get(i).getSzfl());
                    newtree.setNf(list.get(i).getNf());
                    newtree.setDwId(list.get(i).getDwId());
                    newtree.setYjsr(list.get(i).getYjsr());
                    newtree.setYqndyjjzje(list.get(i).getYqndyjjzje());
                    newtree.setYqndsjjzje(list.get(i).getYqndsjjzje());
                    newtree.setSjsr(list.get(i).getSjsr());
                    newtree.setGnkm(list.get(i).getGnkm());
                    newtree.setDw(list.get(i).getDw());
                    newtree.setSfwdlhsdw(list.get(i).getSfwdlhsdw());
                    newtree.setChildren(list.get(i).getChildren());
                    newtree.setGnkmmc(list.get(i).getGnkmmc());
                    newtree.setYhpje(list.get(i).getYhpje());
                    newtree.setYjsrsy(list.get(i).getYjsrsy());
                    newtree.setYqndyjjzyyje(list.get(i).getYqndyjjzyyje());
                    newtree.setYqndyjjzsyje(list.get(i).getYqndyjjzsyje());
                    newtree.setYqndsjjzsyje(list.get(i).getYqndsjjzsyje());
                    newtree.setXmdm(list.get(i).getXmdm());
                    newtree.setYssrfl(list.get(i).getYssrfl());

                    if (list.get(i).getSzfl().equals(dfbkjbzc)){
                        newtree.setDfzcbkjbzc(list.get(i).getYjsr());
                    }else{
                        newtree.setDfzcbkxmzc(list.get(i).getYjsr());
                    }

                    newList.add(newtree);
                    list.get(index).getChildren().addAll(newList);
                    list.remove(i);
                    break;
                }
            }
        }
        return list;
    }


    /**
     * 排序
     * @param list
     * @return
     */
    public List<NonCentralTree> sortFl(List<NonCentralTree> list){
        List<NonCentralTree> newList = new ArrayList<>();
        NonCentralTree tree1 = null;
        NonCentralTree tree2 =null;
        NonCentralTree tree3 = null;
        NonCentralTree tree4 = null;
        NonCentralTree tree5 = null;
        NonCentralTree tree6 = null;
        NonCentralTree tree7 = null;
        NonCentralTree tree8 = null;
        for (NonCentralTree tree :list) {
            if(tree.getTitle().equals("上级补助收入")){
                tree1 = new NonCentralTree();
                tree1 = tree;
            }else if(tree.getTitle().equals("事业收入")){
                tree2 = new NonCentralTree();
                tree2 = tree;
            }else if(tree.getTitle().equals("事业单位经营收入")){
                tree3 = new NonCentralTree();
                tree3 = tree;
            }else if(tree.getTitle().equals("下级单位上缴收入")){
                tree4 = new NonCentralTree();
                tree4 = tree;
            }else if(tree.getTitle().equals("其他收入")){
                tree5 = new NonCentralTree();
                tree5 = tree;
            }else if(tree.getTitle().equals("用事业基金弥补收支差额")){
                tree6 = new NonCentralTree();
                tree6 = tree;
            }else if(tree.getTitle().equals("中央转移支付")){
                tree7 = new NonCentralTree();
                tree7 = tree;
            }else if(tree.getTitle().equals("地方财政拨款")){
                tree8 = new NonCentralTree();
                tree8 = tree;
            }
        }
        if (tree1 !=null)
            newList.add(tree1);
        if (tree2 !=null)
            newList.add(tree2);
        if (tree3 !=null)
        newList.add(tree3);
        if (tree4 !=null)
        newList.add(tree4);
        if (tree5 !=null)
        newList.add(tree5);
        if (tree6 !=null)
        newList.add(tree6);
        if (tree7 !=null)
        newList.add(tree7);
        if (tree8 !=null)
        newList.add(tree8);
        return newList;
    }

    /**
     * 补充数据
     * @param list
     * @return
     */
    public List<NonCentralTree> fill(List<NonCentralTree> list){
        Map<String, Object> map = NonCentralTreeDBUtil.getSrysfl();
        int count = 0;
        Map<Integer,List<NonCentralTree>> countMapz = new HashMap<>();
        for (int i= 0; i < list.size(); i++) {
            if (list.get(i).getSzfl().equals(dfbkjbzc) || list.get(i).getSzfl().equals(dfbkxmzc)){
                if (countMapz.size()==0){
                    List<NonCentralTree> newList = new ArrayList<>();
                    NonCentralTree newtree = new NonCentralTree();
                    newtree.setKey("0-0-0-"+ejKey);
                    newtree.setTitle(list.get(i).getTitle());
                    newtree.setId(list.get(i).getId());
                    newtree.setSzfl(list.get(i).getSzfl());
                    newtree.setNf(list.get(i).getNf());
                    newtree.setDwId(list.get(i).getDwId());
                    newtree.setYjsr(list.get(i).getYjsr());
                    newtree.setYqndyjjzje(list.get(i).getYqndyjjzje());
                    newtree.setYqndsjjzje(list.get(i).getYqndsjjzje());
                    newtree.setSjsr(list.get(i).getSjsr());
                    newtree.setGnkm(list.get(i).getGnkm());
                    newtree.setDw(list.get(i).getDw());

                    ejKey++;
                    count=i;

                    List<NonCentralTree> childrenXm = getchildrenXm(list.get(i).getId());
                    if (!childrenXm.isEmpty())
                        newtree.setChildren(childrenXm);

                    newList.add(newtree);
                    countMapz.put(i,newList);
                    list.get(i).setKey("0-0-"+yjKey);
                    yjKey++;
                    list.get(i).setTitle("地方财政拨款");
                    list.get(i).setChildren(newList);

                    list.get(i).setSjsr("");
                    list.get(i).setYjsr("");
                    list.get(i).setYqndsjjzje("");
                    list.get(i).setYqndyjjzje("");
                    map.remove(list.get(i).getSzfl());
                    continue;
                }else{
//                    List<NonCentralTree> newList = new ArrayList<>();
//                    list.get(i).setKey("0-0-0-"+ejKey);
//                    ejKey++;
//
//                    List<NonCentralTree> childrenXm = getchildrenXm(list.get(i).getId());
//                    if (!childrenXm.isEmpty())
//                    list.get(i).setChildren(childrenXm);

                    List<NonCentralTree> newList = new ArrayList<>();
                    NonCentralTree newtree = new NonCentralTree();
                    newtree.setKey("0-0-0-"+ejKey);
                    newtree.setTitle(list.get(i).getTitle());
                    newtree.setId(list.get(i).getId());
                    newtree.setSzfl(list.get(i).getSzfl());
                    newtree.setNf(list.get(i).getNf());
                    newtree.setDwId(list.get(i).getDwId());
                    newtree.setYjsr(list.get(i).getYjsr());
                    newtree.setYqndyjjzje(list.get(i).getYqndyjjzje());
                    newtree.setYqndsjjzje(list.get(i).getYqndsjjzje());
                    newtree.setSjsr(list.get(i).getSjsr());
                    newtree.setGnkm(list.get(i).getGnkm());
                    newtree.setDw(list.get(i).getDw());

                    List<NonCentralTree> childrenXm = getchildrenXm(list.get(i).getId());
                    if (!childrenXm.isEmpty())
                        newtree.setChildren(childrenXm);

                    newList.add(newtree);
                    list.get(count).getChildren().addAll(newList);
                    map.remove(list.get(i).getSzfl());
                    list.remove(i);
                    continue;
                }

            }
            String key = "0-0-"+ yjKey;
            list.get(i).setKey(key);
            List<NonCentralTree> childrenSjsr = getchildrenSjrj("szfl", list.get(i).getId(), key,1);
            if (!childrenSjsr.isEmpty())
                list.get(i).setChildren(childrenSjsr);

            map.remove(list.get(i).getSzfl());
            yjKey++;
        }

        Map<String,Object> CountMap = new HashMap<>();
        for (Map.Entry<String, Object> entry: map.entrySet()){
            if (entry.getValue().toString().equals("地方拨款基本支出") ||
                    entry.getValue().toString().equals("地方拨款项目支出")){
                CountMap.put(entry.getKey(),entry.getValue().toString());
                continue;
            }else{
                NonCentralTree tree = new NonCentralTree();
                tree.setTitle(entry.getValue().toString());
                tree.setKey("0-0-"+ yjKey);
                tree.setSzfl(entry.getKey());
                yjKey++;
                list.add(tree);
            }
        }
        if (CountMap.size()>1){
             List<NonCentralTree> listxj = new ArrayList<>();
            NonCentralTree tree = new NonCentralTree();
            tree.setTitle("地方财政拨款");
            tree.setKey("0-0-"+ yjKey);
            for (Map.Entry<String,Object> entry:CountMap.entrySet()) {

                NonCentralTree treexj = new NonCentralTree();
                treexj.setTitle(entry.getValue().toString());
                treexj.setKey("0-0-0-"+ ejKey);
                ejKey++;
                treexj.setSzfl(entry.getKey());
                listxj.add(treexj);
                tree.setChildren(listxj);
            }
            list.add(tree);
            yjKey++;
        }else if(CountMap.size() == 1){
            List<NonCentralTree> listxj = new ArrayList<>();
            for (Map.Entry<String,Object> entry:CountMap.entrySet()) {

                NonCentralTree treexj = new NonCentralTree();
                treexj.setTitle(entry.getValue().toString());
                treexj.setKey("0-0-0-"+ ejKey);
                ejKey++;
                treexj.setSzfl(entry.getKey());
                listxj.add(treexj);
                list.get(count).getChildren().addAll(listxj);
            }
        }
        return list;
    }


    /**
     * 根据id获取项目
     * @param id
     * @return
     */
    public List<NonCentralTree> getchildrenXm(String id){
        RecordSet rs = new RecordSet();
        String sql = " select * from "+uf_fzysrxm+" where szfl = "+id+" ";
        rs.executeQuery(sql);
        List<NonCentralTree> list = new ArrayList<>();
        while (rs.next()){
            NonCentralTree tree = new NonCentralTree();
            tree.setTitle(rs.getString("xmmc"));
            String key = "0-0-0-0-"+xmKey;
            tree.setKey(key);
            xmKey++;
            String xmid = rs.getString("id");
            tree.setId(xmid);
            tree.setYjsr(rs.getString("yjsr"));
            tree.setSjsr(rs.getString("sjsr"));
            tree.setYqndyjjzje(rs.getString("yqndyjjzje"));
            tree.setYqndsjjzje(rs.getString("yqndsjjzje"));
            tree.setSfwdlhsdw(rs.getString("sfwdlhsdw"));
            tree.setNf(rs.getString("nf"));
            tree.setDwId(rs.getString("szdw"));
            tree.setYhpje(rs.getString("yhpje"));
            tree.setYjsrsy(rs.getString("yjsrsy"));
            tree.setYqndyjjzyyje(rs.getString("yqndyjjzyyje"));
            tree.setYqndyjjzsyje(rs.getString("yqndyjjzsyje"));
            tree.setYqndsjjzsyje(rs.getString("yqndsjjzsyje"));
            tree.setXmdm(rs.getString("xmdm"));
            tree.setSzfl(rs.getString("szfl"));
            String lfid = NonCentralTreeDBUtil.getLbidById(id);
            tree.setYssrfl(lfid);

            String gnkmId = rs.getString("gnkm");
            if (!gnkmId.isEmpty()){
                String gnkmName = NonCentralTreeDBUtil.getGnkmmcById(gnkmId);
                tree.setGnkm(gnkmId);
                tree.setGnkmmc(gnkmName);
            }else{
                tree.setGnkmmc("");
                tree.setGnkm("");
            }

            int dwid = rs.getInt("szdw");
            RecordSet dw = rmcd.getSBDWById(dwid);
            dw.next();
            String dwStr = dw.getString("SUBCOMPANYNAME");
            tree.setDw(dwStr);

//            List<NonCentralTree> childrenSjsr = getchildrenSjrj("szxm", xmid, key,0);
//            if (!childrenSjsr.isEmpty()){
//                tree.setChildren(childrenSjsr);
//            }
            list.add(tree);
        }
        return list;
    }


    /**
     * 获取实际收入信息
     * @param sjStr 条件字段
     * @param id
     * @param key
     * @param ej //是否二级
     * @return
     */
    public List<NonCentralTree> getchildrenSjrj(String sjStr,String id,String key,Integer ej){
        RecordSet rs = new RecordSet();
        String sql = " select * from "+uf_fzysjsr+" where "+sjStr+" = "+id+" ";
        rs.executeQuery(sql);
        List<NonCentralTree> list = new ArrayList<>();
        int i = 0;
        while (rs.next()){
            NonCentralTree tree = new NonCentralTree();

            if (ej == 1){
                tree.setKey(key+"-"+ejKey);
                ejKey++;
            }else{
                tree.setKey(key+"-"+i);
                i++;
            }
            tree.setId(rs.getString("id"));
            tree.setTitle(rs.getString("srsx"));
            tree.setSjsr(rs.getString("srje"));
            tree.setNf(rs.getString("ysnf"));
            tree.setSrrq(rs.getString("srrq"));
            tree.setLydw(rs.getString("lydw"));

            RecordSet dw = rmcd.getSBDWById(rs.getInt("szdw"));
            dw.next();
            String szdwStr = dw.getString("SUBCOMPANYNAME");
            tree.setDw(szdwStr);
            list.add(tree);
        }
        return list;
    }

    public Map<String,Map<String,List<NonCentralTree>>> queryCtTree(Map<String,Object> param){
        RecordSet rs = new RecordSet();

        Map<String,Map<String,List<NonCentralTree>>> map = new LinkedHashMap<>();


        String SubComPany = "1";
        //if (((Integer)param.get("id")) != Integer.valueOf(sysadmin)){ sysadmin 改为数组后调整
        if (!sysadmin.contains(param.get("id"))){
            //分部
            SubComPany = TreeStuctureDBUtil.getSubCompanyIdByUserId((Integer) param.get("id"))+"";
        }
        //部门
        String Department = TreeStuctureDBUtil.getDepartmentIdByUserId((Integer) param.get("id"));
        //矩阵
        List<String> subDefinedId = TreeStuctureDBUtil.getSubcompanyDefinedByUserId((Integer) param.get("id"));
        //下级分部
        List<String> subDivisionList = TreeStuctureDBUtil.getSupSubcomIdByUserId((Integer) param.get("id"), leaderStr);



        String sql = " select ff.*,fs.mc from "+uf_fzysr+" ff join uf_srysfl fs on ff.szfl = fs.id order by ff.nf desc ";

        int z = 0;
        String uid = param.get("id").toString();
        //if (SubComPany == Integer.valueOf(sysadmin)){
        if (sysadmin.contains(SubComPany)){
                z ++;
        }else if (DepartmentStatus.contains(Department)){
                z ++;
        }else if (status1.contains(uid)){
                z ++;
        }else if (DataBaseUtil.isAuthorityByUid(uid)){
            if (z == 0){
                String bmid = DataBaseUtil.getAuthoritySubIdByUid(uid);
                sql = " select ff.*,fs.mc from "+uf_fzysr+" ff join uf_srysfl fs on ff.szfl = fs.id where dw in("+bmid+") order by ff.nf desc ";
            }
        }else if (status3.contains(uid)){
        }else{
            return map;
        }

        rs.executeQuery(sql);


        for (int i = 0; i < rs.getCounts(); i++) {
            rs.next();

//            if (SubComPany != Integer.valueOf(sysadmin) && !DepartmentStatus.contains(Department) &&
//                    !status1.contains(uid) && !status2.contains(uid) && !status3.contains(uid) &&
//                    !subDefinedId.contains(rs.getString("dw"))){
//                if (subDivisionList.isEmpty()){
//                    continue;
//                }else{
//                    if (!subDivisionList.contains(rs.getString("dw"))){
//                         continue;
//                    }
//                }
//            }

            List<NonCentralTree> list = new ArrayList<>();
            NonCentralTree nct = new NonCentralTree();

            String mc = rs.getString("mc");
            nct.setTitle(mc);
            String id = rs.getString("id");
            nct.setId(id);
            String szfl = rs.getString("szfl");
            nct.setSzfl(szfl);
            nct.setYssrfl(szfl);
            nct.setYjsr(rs.getString("yjsr"));
            nct.setSjsr(rs.getString("sjsr"));
            nct.setYqndyjjzje(rs.getString("yqndyjjzje"));
            nct.setYqndsjjzje(rs.getString("yqndsjjzje"));
            nct.setYhpje(rs.getString("yhpje"));
            nct.setYjsrsy(rs.getString("yjsrsy"));
            nct.setYqndyjjzyyje(rs.getString("yqndyjjzyyje"));
            nct.setYqndyjjzsyje(rs.getString("yqndyjjzsyje"));
            nct.setYqndsjjzsyje(rs.getString("yqndsjjzsyje"));

            String gnkmId = rs.getString("gnkm");
            if (!gnkmId.isEmpty()){
                String gnkmName = NonCentralTreeDBUtil.getGnkmmcById(gnkmId);
                nct.setGnkm(gnkmId);
                nct.setGnkmmc(gnkmName);
            }else{
                nct.setGnkmmc("");
                nct.setGnkm("");
            }

            nct.setSfwdlhsdw(rs.getString("sfwdlhsdw"));
            String nf = rs.getString("nf");
            nct.setNf(nf);

            if (szfl.equals(dfbkjbzc) || szfl.equals(dfbkxmzc)){ //判断是否是地方财政的
                List<NonCentralTree> childrenXm = getchildrenXm(id);
                if (!childrenXm.isEmpty())
                    nct.setChildren(childrenXm);
            }else{
                List<NonCentralTree> childrenXm = getchildrenXm(id);
                if (!childrenXm.isEmpty())
                    nct.setChildren(childrenXm);

                String key =  "0-0-"+yjKey;
                nct.setKey(key);
                yjKey++;
//                List<NonCentralTree> childrenSjsr = getchildrenSjrj("szfl", id, key,1);
//                if (!childrenSjsr.isEmpty())
//                    nct.setChildren(childrenSjsr);
            }

            int dwid = rs.getInt("dw");
            RecordSet dw = rmcd.getSBDWById(dwid);
            dw.next();
            String dwStr = dw.getString("SUBCOMPANYNAME");
            nct.setDw(dwStr);
            nct.setDwId(String.valueOf(dwid));

            list.add(nct);
            String dwname_Id_nf = dwStr+"_"+dwid+"_"+nf;
            if (map.containsKey(nct.getNf())){
                if (map.get(nct.getNf()).containsKey(dwname_Id_nf)){
                    map.get(nct.getNf()).get(dwname_Id_nf).add(nct);
                }else{
                    map.get(nct.getNf()).put(dwname_Id_nf,list);
                }
            }else{
                Map<String,List<NonCentralTree>> dwMap = new HashMap<>();
                dwMap.put(dwname_Id_nf,list);
                map.put(nct.getNf(),dwMap);
            }
        }
        return map;
    }

    //--------------------------------------------------------------------------------------------------------------------------

    /**
     *新增
     * @param param
     * @return
     */
    @POST
    @Path("/AddCentralTree")
    @Produces(MediaType.APPLICATION_JSON)
    public String AddCentralTree(Map<String,Object> param){
        Integer grade = (Integer) param.get("grade");
        String isSuccess ;
        if (grade==1){
            boolean is = NonCentralTreeDBUtil.addNonCt(param,uf_fzysr);
            isSuccess = String.valueOf(is);
        }else{
            boolean is = NonCentralTreeDBUtil.addNonCtXm(param,uf_fzysrxm);
            isSuccess = String.valueOf(is);
        }
        return JSON.toJSONString(isSuccess);
    }

    //--------------------------------------------------------------------------------------------------------------------------


    /**
     * 新增实际收入
     * @param param
     * @return
     */
    @POST
    @Path("/AddrealIncome")
    @Produces(MediaType.APPLICATION_JSON)
    public String AddrealIncome(Map<String,Object> param){
        Integer grade = (Integer) param.get("grade");
        String isSuccess;
        if (grade==1){ //新增类别下实际收入
            isSuccess = addNonSjsr(param,uf_fzysjsr);
        }else{//新增项目下实际收入
            isSuccess = addNonSjsr(param,uf_fzysjsr);
        }
        return JSON.toJSONString(isSuccess);
    }

    /**
     * 添加非中央实际收入
     * @param param
     * @param uf_fzysjsr
     * @return
     */
    public static String addNonSjsr(Map<String,Object> param,String uf_fzysjsr){
        param.remove("grade");

        RecordSetTrans rs = new RecordSetTrans();
        rs.setAutoCommit(false);

        try {
            String lbid = getfzyIdByxmIdSql(param.get("szxm").toString());
            param.put("szfl",lbid);

            String uuid = UUID.randomUUID().toString();
            Integer modeId = DataBaseUtil.getModeId(uf_fzysjsr);
            String[] names =  param.keySet().toArray(new String[param.size()]);
            Object[] values = param.values().toArray();

            String modeInsertSql = DataBaseUtil.getModeInsertSql(Arrays.asList(names), uf_fzysjsr, uuid, modeId);

            boolean IsSuccess = rs.executeUpdate(modeInsertSql, values);
            boolean istSuccess = false;
            if (IsSuccess){
                //获取实际收入的和 //dlt 21.12.1
//                String sjsrje = NonCentralTreeDBUtil.getSjsrJeByXmid(param.get("szxm").toString());

                String sjsrje = param.get("srje").toString();
                String addSjsrSql = addSjsrSql2(param.get("szxm").toString(), "uf_fzysrxm", sjsrje);

                boolean isSuccess =  rs.executeUpdate(addSjsrSql);

                String srid = NonCentralTreeApi.getfzyIdByxmId(param.get("szxm").toString());
                String xmsjsrje = NonCentralTreeDBUtil.getSjsrJe(srid);

//                String addSjsrSql2 = addSjsrSql2(lbid, "uf_fzysr",xmsjsrje);
                String addSjsrSql2 = addSjsrSql2(lbid, "uf_fzysr",sjsrje);
                boolean isSuccess2 =  rs.executeUpdate(addSjsrSql2);

//                    String fzyIdByxmIdSql = getfzyIdByxmIdSql(param.get("szxm").toString());
//                    rs.executeQuery(fzyIdByxmIdSql);
//                    rs.next();
//                    String lbid = rs.getString("szfl");

                    if (isSuccess&&isSuccess2){//插入日志
                        String yjsrfl = NonCentralTreeDBUtil.getLbidByxmId(param.get("szxm").toString()); //预计收入分类
                        Object sfczbk = param.containsKey("sfczbk")?param.get("sfczbk"):"";
                        Object gnkm = param.containsKey("gnkm")?param.get("gnkm"):"";
                        Object gnkmmc = param.containsKey("gnkmmc")?param.get("gnkmmc"):"";
                        String istSql = " insert into uf_fzyrzb (nf,szxm,rq,dw,sfczbk,srsx,lydw,srje,gnkm,gnkmmc,yjsrfl) values" +
                                " ("+param.get("nf")+","+param.get("szxm")+",'"+ Utils.getThisDate() +"',"+param.get("szdw")+"" +
                                " ,'"+sfczbk+"','"+param.get("srsx")+"','"+param.get("lydw")+"',"+param.get("srje")+",'"+gnkm+"' " +
                                " ,'"+gnkmmc+"',"+yjsrfl+")";

                        istSuccess = rs.executeUpdate(istSql);
                        if (istSuccess){
                            rs.commit();
                            return "true";
                        }else{
                            rs.rollback();
                            return "false";
                        }
                    }else{
                        rs.rollback();
                        return "false";
                    }
            }else{
                rs.rollback();
                return "false";
            }
        } catch (Exception e) {
            rs.rollback();
            e.printStackTrace();
        }
        return "false";
    }

    /**
     * 添加实际收入sql  累加
     * @param id
     * @param tbName
     * @param je
     * @return
     */
    public static String addSjsrSql2(String id,String tbName,String je){
        String sql =" update  "+tbName+" set sjsr = sjsr + "+je+",sjsrsy = sjsrsy +"+je+" where id = "+id+" ";
        return sql;
    }

    /**
     * 添加实际收入 求和
     * @param id
     * @param tbName
     * @param je
     * @return
     */
    public static String addSjsrSql(String id,String tbName,String je){
        String sql =" update  "+tbName+" set sjsr ="+je+",sjsrsy = "+je+" where id = "+id+" ";
        return sql;
    }

    /**
     * 根据项目id获取类别id
     * @param id
     * @return
     */
    public static String getfzyIdByxmId(String id){
        RecordSet rs = new RecordSet();
        String sql = " select szfl from uf_fzysrxm where id = "+id+" ";
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("szfl");
    }

    /**
     * 根据项目id获取类别id
     * @param id
     * @return
     */
    public static String getfzyIdByxmIdSql(String id){
        RecordSet rs = new RecordSet();
        String sql = " select szfl from uf_fzysrxm where id = "+id+" ";
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("szfl");
    }


    //--------------------------------------------------------------------------------------------------------------------------

    /**
     * 修改
     * @param param
     * @return
     */
    @POST
    @Path("/updateCtTree")
    @Produces(MediaType.APPLICATION_JSON)
    public String updateCtTree(Map<String,Object> param){
        Integer grade = (Integer) param.get("grade");
        String isSuccess;
        if (grade==1){
            boolean is = NonCentralTreeDBUtil.modifyFzysr2(param,uf_fzysr);
            isSuccess = String.valueOf(is);
        }else{
            String xmid = param.get("id").toString();
            boolean is = NonCentralTreeDBUtil.upfzysrxm2(param,uf_fzysrxm);
            isSuccess = String.valueOf(is);
        }
        return JSON.toJSONString(isSuccess);
    }

    //--------------------------------------------------------------------------------------------------------------------------

    /**
     * 删除
     * @param param
     * @return
     */
    @POST
    @Path("/deleteCtTree")
    @Produces(MediaType.APPLICATION_JSON)
    public String deleteCtTree(Map<String,Object> param){
        Integer grade = (Integer) param.get("grade");
        String isSuccess;
        if (grade==1){ //删除收入
            boolean is = NonCentralTreeDBUtil.defzysr(param,uf_fzysr,uf_fzysrxm);
            isSuccess = String.valueOf(is);
        }else if(grade==3){ //删除实际收入
            boolean is = NonCentralTreeDBUtil.defzysjsr(param, uf_fzysjsr);
            isSuccess = String.valueOf(is);
        }else{ //删除项目
            boolean is = NonCentralTreeDBUtil.defzysrxm(param,uf_fzysrxm);
            isSuccess = String.valueOf(is);
        }
        return JSON.toJSONString(isSuccess);
    }


    //------------------------------------------------------------------------------------------------------------------实际收入

    /**
     * 获取实际收入列表
     * @param param
     * @return
     */
    @POST
    @Path("/getSjsrList")
    @Produces(MediaType.APPLICATION_JSON)
    public String getSjsrList(Map<String,Object> param){
        List<RealIncome> list = NonCentralTreeDBUtil.getSjsrList(uf_fzysjsr, param.get("id").toString());
        return JSON.toJSONString(list);
    }


    //--------------------------------------------------------------------------------------------------------------------------

    /**
     * 查看日志
     * @param param
     * @return
     */
    @POST
    @Path("/queryLog")
    @Produces(MediaType.APPLICATION_JSON)
    public String queryLog(Map<String,Object> param){
        Integer grade = (Integer) param.get("grade");
        String isSuccess;
        if (grade==1){
            List<NonCentralLog> logInfo = getLogInfo("szlb", param.get("id").toString());
            return JSON.toJSONString(logInfo);
        }else{
            List<NonCentralLog> logInfo = getLogInfo("szxm", param.get("id").toString());
            return JSON.toJSONString(logInfo);
        }
    }

    /**
     * 获取日志信息
     * @param fieldName
     * @param id
     * @return
     */
    public List<NonCentralLog> getLogInfo(String fieldName,String id){
        RecordSet rs = new RecordSet();
        String sql = " select * from  uf_fzyrzb where "+fieldName+" = "+id+" ";
        rs.executeQuery(sql);
        List<NonCentralLog> list = new ArrayList<>();
        while (rs.next()){
            NonCentralLog treeLog = new NonCentralLog();
            treeLog.setYsnf(rs.getString("nf"));

            int dwid = rs.getInt("dw");
            RecordSet dw = rmcd.getSBDWById(dwid);
            dw.next();
            String dwStr = dw.getString("SUBCOMPANYNAME");
            treeLog.setSzdw(dwStr);

            String yjsrfl = getFlNameById(rs.getString("yjsrfl"));
            treeLog.setSzfl(yjsrfl);
            treeLog.setGnkm(rs.getString("gnkmmc"));
            treeLog.setSfczbk(rs.getString("sfczbk"));
            treeLog.setSrsx(rs.getString("srsx"));
            treeLog.setLydw(rs.getString("lydw"));
            treeLog.setSrrq(rs.getString("rq"));
            treeLog.setSrje(rs.getString("srje"));
            list.add(treeLog);
        }
        return list;
    }

    /**
     * 根据类别id获取类别名称
     * @param id
     * @return
     */
    public static String getFlNameById(String id){
        RecordSet rs = new RecordSet();
        String sql = " select mc from uf_srysfl where id = "+id+" ";
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("mc");
    }

    @POST
    @Path("/isDestatus")
    @Produces(MediaType.APPLICATION_JSON)
    public static String isDestatus(Map<String,Object> param){
        String id = param.get("id").toString();
        RecordSet rs=  new RecordSet();
        String sql = " select departmentmark from HrmDepartment  where id = "+id+"  ";
        rs.executeQuery(sql);
        rs.next();
        String departmentmark = rs.getString("departmentmark");
        if (departmentmark.equals("预算管理科")){
            return JSON.toJSONString("true");
        }else{
            return JSON.toJSONString("false");
        }
    }
}
